import { addCargoAPI, deleteCargoAPI, fetchCargoListAPI, fetchCargoTypeAPI, updateCargoAPI } from '@/api/business/cargoAPI'

const actions = {
  /*
   * 获取货品列表的action
   *  */
  async fetchCargoListAction(context, id) {
    const { data } = await fetchCargoListAPI(id)
    context.commit('setCargoListMutation', data)
  },
  /*
  *
  * 删除货物的action
  *  */
  async deleteCargoAction(context, id) {
    await deleteCargoAPI(id)
    return '货物删除成功'
  },
  /*
  *
  * 获取货物类型信息列表的action
  *  */
  async fetchCargoTypeAction(context) {
    const { data } = await fetchCargoTypeAPI()
    context.commit('setCargoTypeMutation', data)
  },
  /*
  * 添加货物的action
  *  */
  async addCargoAction(context, formData) {
    await addCargoAPI(formData)
    return '货物添加成功'
  },
  /*
  * 更新货物的action
  *  */
  async updateCargoAction(context, formData) {
    await updateCargoAPI(formData)
    return '货物更新成功'
  }
}

const mutations = {
  /*
 * 设置货品列表的mutation
 *  */
  setCargoListMutation(state, cargoList) {
    state.cargoList = cargoList
  },
  /*
  *
  *  */
  setCargoTypeMutation(state, cargoType) {
    state.cargoType = cargoType
  }
}
const state = () => {
  return {
    cargoList: [],
    cargoType: []
  }
}

export default {
  actions,
  mutations,
  state,
  namespaced: true
}
